<?php
/**
* 2017年4月10日 11:20:23
* 系统日志
* author:silenceluo
* email:10955840812qq.com
*/
class SystemLog_model extends MY_Model
{
	//日志数据
	private $log = null;

	private $logContent = null;//添加日志 信息 cp_log表的

	private $table = 'log';

	private $tableDetails = 'log_details';
	//查询字段
	private $select = null;
	//查询条件
	private $where = null;

	function __construct()
	{	
		parent::__construct();
	}
	/**
	 * 设置日志内容
	 * @param [type] $log [description]
	 */
	function setLog($log)
	{
		$this->log = $log;
	}
	/**
	 * 设置查询字段
	 * @param [type] $select [description]
	 */
	function setSelect($select)
	{
		$this->select = $select;
	}
	/**
	 * 设置查询条件
	 * @param [type] $where [description]
	 */
	function setWhere($where)
	{
		$this->where = $where;
	}
	/**
	 * 添加日志
	 * 2017年4月10日 11:37:30
	 */
	function addLog($log)
	{	
		$this->setLog( $log );
		return $this->hw_Insert($this->table, $this->log);
	}
	/**
	 * 添加日志详情
	 * 2017年4月10日 11:44:05
	 */
	function addLogDetails($details, $log_id)
	{
		$this->setLog( array('details' => $details, 'logid' => $log_id ) );
		$this->hw_Insert($this->tableDetails, $this->log);
	}
	/**
	 * 获取日志方法
	 * @param  [type] $tablename [description]
	 * @param  [type] $table_id  [description]
	 * @return [type]            [description]
	 */
	function getLog($tablename,$table_id)
	{
		$this->setWhere( 
				array(
				  'tablename' 	=> $tablename,
				  'keys' 		=> $table_id,
				  'status'		=> 1,
				) 
			);
		$this->setSelect('*');

		return $this->hw_getList($this->table, $this->where, $this->select);
	}
	/**
	 * 获取最后修改信息
	 * @return [type] [description]
	 */
	function getLastLog($tablename,$table_id)
	{
		$this->setWhere(
				array(
				  'tablename' 	=> $tablename,
				  'keys' 		=> $table_id,
				  'status'		=> 1,
				) 
			);
		$this->setSelect('operator,desc,create_time');
		
		return $this->hw_getRecord($this->table, $this->where, $this->select,'create_time desc');
	}


	/**
	 * 添加日志
	 * 2017年4月18日 15:55:13 谢
	 * @param  [type] $tablename  [操作表名]
	 * @param  [type] $status  	  [操作结果，0失败，1成功]
	 * @param  [type] $type    	  [操作类型，0删除，1新增，2编辑，3审核，4拆分，5合并]
	 * @param  [type] $keys    	  [键值，操作表的具体id]
	 * @param  [type] $details 	  [操作信息]
	 * @return [type]          	  [description]
	 */
	function addOperationLog($status, $type, $keys, $tablename,$tabledesc = '详情', $details = null, $desc = '')
	{
		$this->logContent['tablename'] 	= $tablename;
		$this->logContent['operatorid'] = $_SESSION['admin_info']['id'];
		$this->logContent['operator']	= $_SESSION['admin_info']['name'];
		$this->logContent['status'] 	= $status;
		$this->logContent['type']		= $type;
		$this->logContent['keys']		= $keys;
		$this->logContent['desc']		= $desc==''?$_SESSION['admin_info']['name'].$this->getLogDesc($type,$tabledesc):$desc;

		//$this->load->model('SystemLog_model','syslog');
		$log_id = $this->addLog($this->logContent);
		if( !is_null($details) && $log_id)
		{
			$this->addLogDetails($details,$log_id);
		}
	}

	/**
	 * 添加日志-机构后台
	 * 2017年4月18日 15:55:13 谢
	 * @param  [type] $tablename  [操作表名]
	 * @param  [type] $status  	  [操作结果，0失败，1成功]
	 * @param  [type] $type    	  [操作类型，0删除，1新增，2编辑，3审核，4拆分，5合并]
	 * @param  [type] $keys    	  [键值，操作表的具体id]
	 * @param  [type] $details 	  [操作信息]
	 * @return [type]          	  [description]
	 */
	function addOperationLogOrg($status, $type, $keys, $tablename,$tabledesc = '详情', $details = null, $desc = '')
	{
		$this->logContent['tablename'] 	= $tablename;
		$this->logContent['operatorid'] = $_SESSION['org_info']['account_id'];
		$this->logContent['operatortype'] = 1;
		$this->logContent['operator']	= $_SESSION['org_info']['username'];
		$this->logContent['status'] 	= $status;
		$this->logContent['type']		= $type;
		$this->logContent['keys']		= $keys;
		$this->logContent['desc']		= $desc==''?$_SESSION['org_info']['username'].$this->getLogDesc($type,$tabledesc):$desc;

		//$this->load->model('SystemLog_model','syslog');
		$log_id = $this->addLog($this->logContent);
		if( !is_null($details) && $log_id)
		{
			$this->addLogDetails($details,$log_id);
		}
	}

	 /**
     * 获取日志描述内容
     * @param  [type] $type [description]
     * @param  [type] $desc [description]
     * @return [type]       [description]
     */
    function getLogDesc($type,$desc)
    {
        switch ($type) {
            case 0:
                //删除日志
                $desc = "删除了该".$desc;
                break;
            
            case 1:
                //新增日志
                $desc = "添加了该".$desc;
                break;

            case 2:
                //编辑日志
                $desc = "编辑了该".$desc;
                break;

            case 3:
                //审核日志
                $desc = "审核了该".$desc;
                break;

            case 4:
                //拆分日志
                $desc = "拆分了该".$desc;
                break;

            case 5:
                //合并日志
                $desc = "合并了该".$desc;
                break;
        }

        return $desc;
    }
    
    /**
     * 获取日志详细数据
     * @param  [type] $logid [description]
     * @return [type]        [description]
     */
    function getDetails($logid)
    {
    	$this->setWhere( array('logid'=>$logid) );
    	$this->setSelect('*');

    	return $this->hw_getRecord($this->tableDetails,$this->where,$this->select);
    }

    /**
	 * [获取该数据日志信息]
	 * @AuthorHTL
	 * @DateTime  2017-06-06T09:58:43+0800
	 * @param     [type] $table [操作表]
	 * @param     [type] $id    [数据id]
	 * @return    [type] $list  [日志]
	 */
	function getLogList($table, $id)
	{
		$where = array(
			'tablename' => $table,
			'keys' => $id,
			);
		$list = $this->hw_getList('log', $where, '*');
		if ($list) {
			foreach ($list as $key => $value) {
				switch ($value['type']) {
					case 0:
						$make = '删除了该数据';
						break;
					
					case 1:
						$make = '新增了该数据';
						break;

					case 2:
						$make = '修改了该数据';
						break;
				}
				// 设置操作内容
				$list[$key]['desc'] = $value['operator'].$make;

				// 获取该操作的操作数据
				$where = array('logid' => $value['id']);
				$detail = $this->hw_getRecord('log_details', $where, 'details', 'id desc');
				$list[$key]['log_details']['details'] = $detail['details'];
			}
			$return = array(
				'code' => 1,
				'msg' => '日志获取成功',
				'log' => $list,
				);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '日志获取失败',
				);
		}

		return $return;
	}
}